package com.Iconcat.system.mapper;

import com.Iconcat.system.entity.CatUser;
import com.Iconcat.system.entity.DrawRecord;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

@Mapper
public interface CatUserMapper extends BaseMapper<CatUser> {
    IPage<CatUser> userAll(Page<CatUser> page, @Param(Constants.WRAPPER) QueryWrapper<CatUser> wrapper);
    @Update("UPDATE cat_user " +
            "SET draw_count = draw_count - 1 " +
            "WHERE userid = #{userId} AND draw_count > 0")
    int updateDrawCountByUserId(@io.lettuce.core.dynamic.annotation.Param("userId") Integer userId);

    @Update("UPDATE cat_user SET available_balance=available_balance+#{totalEarnings}  WHERE userid = #{userId}")
    BigDecimal addBalance(@Param("userId") Integer userId,@Param("totalEarnings") BigDecimal totalEarnings);

    /**
     * 根据用户ID查询抽奖次数
     * @param userId 用户ID
     * @return 抽奖次数
     */
    @Select("SELECT draw_count FROM cat_user WHERE userid = #{userId}")
    Integer selectDrawCountByUserId(@Param("userId") Integer userId);

    @Select("SELECT 1 FROM cat_user WHERE user_name = #{userName} LIMIT 1")
    Integer findExistsByUsername(@Param("userName") String userName);
    @Update("<script>" +
            "UPDATE cat_user " +
            "SET available_balance = available_balance - #{deductAmount} " +
            "WHERE userid = #{userId}" +
            "</script>")
    int updateBalance(@Param("userId") Integer userId, @Param("deductAmount") BigDecimal deductAmount);
}
